package com.mylib.libcore.utils;

import android.content.Context;
import android.content.SharedPreferences;

import com.google.gson.Gson;
import com.mylib.libcore.bean.EveryDayConfigBean;
import com.mylib.libcore.bean.UserInfoBean;

import static com.mylib.libcore.config.AppSPKeyConfig.NEWS_KEY;
import static com.mylib.libcore.config.AppSPKeyConfig.NEWS_ZX_KEY_ID;


public class AppPreferencesHelper {

    public final static String KEY_SP_SYS_STATE = "key_sp_sys_state";
    public final static String KEY_SP_USER_PHONE = "key_sp_user_phone";
    public final static String ISLOGIN = "ISLOGIN";

    public final static String NOTOKENCARGOODS = "NOTOKENCARGOODS";
    private String prefFileName;
    private final SharedPreferences mPrefs;
    private final Gson mGson;

    public AppPreferencesHelper(Context context) {
        mGson = JsonUtil.gsonBuilder.create();
        prefFileName = context.getPackageName() + "_preferences";
        mPrefs = context.getSharedPreferences(prefFileName, Context.MODE_PRIVATE);
    }

    //清空缓存
    public void clearPreferencesHelper(){
        mPrefs.edit().clear().apply();
    }

    //缓存关键字
    public void setKeyValue(String key,String value){
        mPrefs.edit().putString(key, value).apply();
    }
    //获取关键字
    public String getKeyValue(String key){
        return mPrefs.getString(key, "")+"";
    }

    //缓存任务提交次数
    public void setSubmitTaskNum(int value){
        mPrefs.edit().putInt("submit_read_num", value).apply();
    }
    //获取任务提交次数
    public Integer getSubmitTaskNum(){
       try {
           return mPrefs.getInt("submit_read_num", 0);
       }catch (Exception e){
           return 0;
       }
    }



    //缓存关键字
    public void setToken(String value){
        mPrefs.edit().putString("token", value).apply();
    }
    //获取关键字
    public String getToken(){
        String tk = mPrefs.getString("token", "");
        if(UIUtils.isEmpty(tk)) return "";
        else return tk;
    }


    //设置开始时间
    public void setAppStartTime(){
        try {
            mPrefs.edit().putString("app_start_time",System.currentTimeMillis()/1000+"").apply();
        }catch (Exception e){}
    }
    //获取开始时间
    public String getAppStartTime(){
        try {
            return mPrefs.getString("app_start_time",System.currentTimeMillis()/1000+"")+"";
        }catch (Exception e){
            return System.currentTimeMillis()/1000+"";
        }

    }




    //设置用户信息
    public void setUserInfoBean(UserInfoBean bean){
        if(UIUtils.isEmpty(bean)) return;
        mPrefs.edit().putString("user_id", bean.getId()+"").apply();
        mPrefs.edit().putString("user_info", JsonUtil.toJson(bean)).apply();
    }
    //获取用户信息
    public UserInfoBean getUserInfoBean(){
        String user_info_json = mPrefs.getString("user_info", "");
        if(!UIUtils.isEmpty(user_info_json)){
            UserInfoBean bean = (UserInfoBean)JsonUtil.parseJsonToBean(user_info_json,UserInfoBean.class);
            if(!UIUtils.isEmpty(bean) ){
                return bean;
            }else {
                return  getNewUserInfo();
            }
        }else {
            return  getNewUserInfo();
        }
    }

    private UserInfoBean  getNewUserInfo(){
        UserInfoBean bean = new UserInfoBean();
        bean.setId(0);
        bean.setExLv(1);
        bean.setpUserId("");
        bean.setOtLv("");
        bean.setMemoNum(0);
        bean.setDescription("");
        bean.setDeviceNum("-");
        bean.setDt("");
        bean.setEmigratedNum(0);
        bean.setExperience(0);
        bean.setGoldNum("0");
        bean.setFightNum(0);
        bean.setHeadPic("");
        bean.setInvitationCode("");
        bean.setLvTitle("");
        bean.setNickName("--");
        bean.setOnlineTime(0);
        bean.setSex(0);
        bean.setWalkNum(0);
        bean.setIsSign(0);
        return bean;
    }


    //设置用户每日配置
    public void setEveryDayConfig(EveryDayConfigBean bean){
        if(UIUtils.isEmpty(bean)){
            return;
        }
        mPrefs.edit().putString("every_day_config", JsonUtil.toJson(bean)).apply();
    }
    //获取用户每日配置
    public EveryDayConfigBean getEveryDayConfig(){
        String user_info_json = mPrefs.getString("every_day_config", "");
        if(!UIUtils.isEmpty(user_info_json)){
            EveryDayConfigBean bean = (EveryDayConfigBean)JsonUtil.parseJsonToBean(user_info_json,EveryDayConfigBean.class);
            if(!UIUtils.isEmpty(bean) ){
                return bean;
            }else {
                return  getNewDayConfig();
            }
        }else {
            return  getNewDayConfig();
        }
    }

    private EveryDayConfigBean  getNewDayConfig(){
        EveryDayConfigBean bean = new EveryDayConfigBean();
        bean.setClockTag(0);
        bean.setEmigratedNum(0);
        bean.setFightNum(0);
        bean.setLotteryNum(0);
        bean.setSignTag(0);
        bean.setTaskNum(0);
        return bean;
    }



    //设置坐标经纬度
    public void setLocation(String latitude,String longitude){
        mPrefs.edit().putString("map_location", latitude+"="+longitude).apply();
    }
    //获取坐标
    public String getLocation(){
        return mPrefs.getString("map_location", "")+"";
    }

    public Boolean getKeyBoolean(String key){
        return mPrefs.getBoolean(key, false);
    }

    public void setKeyBoolean(String key,Boolean value){
        mPrefs.edit().putBoolean(key, value).apply();
    }


    public void setNoTokenCarGoods(String value){
        mPrefs.edit().putString(NOTOKENCARGOODS, value).apply();
    }
    public String getNoTokenCarGoods(){
        return mPrefs.getString(NOTOKENCARGOODS, "")+"";
    }


    public void setSysState(String sysState) {
        mPrefs.edit().putString(KEY_SP_SYS_STATE, sysState).apply();
    }

    public String getSysState() {
        return mPrefs.getString(KEY_SP_SYS_STATE, "")+"";
    }
    public void setUserPhone(String phone) {
        mPrefs.edit().putString(KEY_SP_USER_PHONE, phone).apply();
    }

    public String getUserPhone() {
        return mPrefs.getString(KEY_SP_USER_PHONE, "");
    }


    //音乐播放模式
    public void setMusicPlayType(String  key,int value){
        mPrefs.edit().putInt(key, value).apply();
    }
    public int getMusicPlayType(String key){
        return mPrefs.getInt(key, 0);
    }


}
